/*
 @file    mainwindow.h
 @author  Rossella Petrucci, Alvise Rigo, Alberto Rubin
*/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVTKWidget.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/point_picking_event.h>
#include <pcl/visualization/interactor.h>
#include <vtkSmartPointer.h>
#include <pcl/registration/transforms.h>
#include <pcl/keypoints/sift_keypoint.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/search/kdtree.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/fpfh_omp.h>
#include <pcl/features/pfh.h>
#include <pcl/features/pfhrgb.h>
#include <pcl/features/3dsc.h>
#include <pcl/features/shot_omp.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/kdtree/impl/kdtree_flann.hpp>
#include <pcl/registration/transformation_estimation_svd.h>
#include <pcl/registration/icp.h>
#include <pcl/registration/correspondence_rejection_sample_consensus.h>
#include <pcl/common/transforms.h>
#include <pcl/surface/grid_projection.h>
#include <pcl/surface/gp3.h>
#include <pcl/surface/marching_cubes_greedy.h>
#include <pcl/surface/marching_cubes_greedy_dot.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/sample_consensus/sac_model_line.h>

#include <stdio.h>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QFileDialog;
class QString;
class QAction;
class QMenu;
class QEvent;
class QComboBox;
class QTextEdit;
class QSpinBox;
class QScrollBar;
class QGroupBox;
class QRadioButton;
class QButtonGroup;
class QAbstractButton;

/*! This is the main class of the project.
 */
class MainWindow : public QMainWindow{

	Q_OBJECT

public:

	/*! Constructor of the class MainWindow. */
    MainWindow();

 protected:
     //void closeEvent(QCloseEvent *event);

private slots:

	/*! Loads a source pcd file. */
	void openFile();

	/*! Loads a target pcd file. */
	void openTarget();

	/*! Segments source and target point cloud. */
	void segment();

	/*! Shows the selected pcd file.
	 \param[in] index index of the selected item.
	*/
	void selectVis(int index);

	/*! Registers source point cloud. */
	void registers();

	/*! Creates new window that displays the selected component. */
	void newWindow();

	/*! Selects the component. */
	void selectComponent();

	/*! Saves the selected component in a pcd file. */
	void saveComponent();

	/*! Shows informations about application. */
	void about();

	/*! Checks if a selected component is coplanar. */
	void coplanarityCheck();

	/*! Opens a form where user can configure the application's parameters. */
	void options();

	/*! Gets the user's settings. */
	void confirmOptions();

	/*! Loads a stored component from pcd files. */
	void loadComponent();

	/*! Finds and checks the cable on Cobra. */
	void cableCheck();

	/*! Checks if a selected component in the target is present in the source. */
	void presenceCheck();

	/*! Listener associated to radio buttons.
	 \param[in] index index of the selected item.
	*/
	void chooseCheck(int index);

	/*! Sets the angle's threshold for coplanarity check.
	 \param[in] threshold threshold new angle threshold.
	*/
	void updateAngleTollerance(int threshold);

	/*! Selects the target ribbon cable. */
	void selectRibbonCable();

	/*! Checks if a selected ribbon cable is in the correct position in the source. */
	void ribbonCableCheck();

private:
	/*! Initializes the various components. */
	void init();

	/*! Creates the actions associated with buttons. */
	void createActions();

	/*! Creates the menù. */
	void createMenus();

	/*! Creates the tool bars. */
	void createToolBars();

	/*! Creates the status bar. */
	void createStatusBar();

	/*! Displays the point cloud p.
	 \param[in] p point cloud to display.
	 \param[in] s name of the point cloud.
	*/
	void visualize(pcl::PointCloud<pcl::PointXYZRGB>::Ptr p, QString s);

	/*! Writes the string in the console.
	 \param[in] s string to visualize in the console.
	*/
	void sendConsole(QString);

	static void pointPickCallback(const pcl::visualization::PointPickingEvent& event, void* cookie);
	static void pointPickCallbackCheckPresence(const pcl::visualization::PointPickingEvent& event, void* cookie);
	static void pointPickCallbackCheckRibbon(const pcl::visualization::PointPickingEvent& event, void* cookie);

	/*! Specifies the voice of the combobox to enable/disable.
	 \param[in] index index of the item to enable/disable
	 \param[in] enabled boolean value to enable/disable the selected item
	*/
	void setCombobox(int index, bool enabled);

	/*! Restores the initial configuration of the application.
	 \sa MainWindow::resetPresence() , MainWindow::resetCoplanarity()
	*/
	void resetAll();

	/*! Resets the settings of presence check. */
	void resetPresence();

	/*! Resets the settings of coplanarity check. */
	void resetCoplanarity();

	/*! Enable/Disable the console's updates.
	 \param c boolean value used for enable/disable console's updates
	 \sa MainWindow::sendConsole(QString s)
	*/
	void setConsole(bool c);


	pcl::visualization::PCLVisualizer *viewer;
	QLabel *label;
	QLabel *label2;
	QLabel *labelFileName;
	QLabel *title;
	QPushButton *checkCoplanarity;
	QTextEdit *console;
	QString fileName;
	QString sourceFileName;
	QComboBox *selVis;
	QScrollBar *scrollV;
	QScrollBar *scrollH;
	QVTKWidget *qvtkVisualizer;
	QCheckBox *checkbox;
	QRadioButton *radio1;
	QRadioButton *radio2;
	QRadioButton *radio3;
	QGroupBox *f2;
	QButtonGroup *bgroup;
	QSpinBox *angleTollerance;

	//MENU'
	QMenu *fileMenu;
	QMenu *runMenu;
	QMenu *checkMenu;
	QMenu *helpMenu;

	QToolBar *fileToolBar;
	QToolBar *runToolBar;
	QToolBar *checkToolBar;

	//Azioni menù "File"
	QAction *openAct;
	QAction *openTargetAct;
	QAction *saveAct;
	QAction *loadComponentAct;
	QAction *exitAct;

	//Azioni menù "Run"
	QAction *segmentationAct;
	QAction *registrationAct;
	QAction *settingAct;

	//Azioni menù "Check"
	QAction *startSelectionAct;
	QAction *stopSelectionAct;
	QAction *cableCheckAct;
	QAction *ribbonCableAct;

	//Azioni menù "Help"
	QAction *helpAct;

	QString curFile;

 };

 #endif
